//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class DiscountDBManager
    {
        #region Methods
        public static Discount GetByDiscountID(int DiscountID)
        {
            Discount discount = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    discount = new Discount();
                    discount.DiscountID = NopDataHelper.GetInt(dataReader, "DiscountID");
                    discount.Name = NopDataHelper.GetString(dataReader, "Name");
                    discount.UsePercentage = NopDataHelper.GetBoolean(dataReader, "UsePercentage");
                    discount.DiscountPercentage = NopDataHelper.GetDecimal(dataReader, "DiscountPercentage");
                    discount.DiscountAmount = NopDataHelper.GetDecimal(dataReader, "DiscountAmount");
                    discount.StartDate = NopDataHelper.GetDateTime(dataReader, "StartDate");
                    discount.EndDate = NopDataHelper.GetDateTime(dataReader, "EndDate");
                    discount.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                }
            }
            return discount;
        }

        public static DiscountCollection GetAllDiscounts(bool showHidden)
        {

            DiscountCollection discountCollection = new DiscountCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Discount discount = new Discount();
                    discount.DiscountID = NopDataHelper.GetInt(dataReader, "DiscountID");
                    discount.Name = NopDataHelper.GetString(dataReader, "Name");
                    discount.UsePercentage = NopDataHelper.GetBoolean(dataReader, "UsePercentage");
                    discount.DiscountPercentage = NopDataHelper.GetDecimal(dataReader, "DiscountPercentage");
                    discount.DiscountAmount = NopDataHelper.GetDecimal(dataReader, "DiscountAmount");
                    discount.StartDate = NopDataHelper.GetDateTime(dataReader, "StartDate");
                    discount.EndDate = NopDataHelper.GetDateTime(dataReader, "EndDate");
                    discount.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    discountCollection.Add(discount);
                }
            }

            return discountCollection;
        }

        public static Discount InsertDiscount(string Name, bool UsePercentage, decimal DiscountPercentage, decimal DiscountAmount,
            DateTime StartDate, DateTime EndDate, bool Deleted)
        {
            Discount discount = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountInsert");
            db.AddOutParameter(dbCommand, "DiscountID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "UsePercentage", DbType.Boolean, UsePercentage);
            db.AddInParameter(dbCommand, "DiscountPercentage", DbType.Decimal, DiscountPercentage);
            db.AddInParameter(dbCommand, "DiscountAmount", DbType.Decimal, DiscountAmount);
            db.AddInParameter(dbCommand, "StartDate", DbType.DateTime, StartDate);
            db.AddInParameter(dbCommand, "EndDate", DbType.DateTime, EndDate);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int DiscountID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@DiscountID"));
                discount = GetByDiscountID(DiscountID);
            }
            return discount;
        }

        public static Discount UpdateDiscount(int DiscountID, string Name, bool UsePercentage, decimal DiscountPercentage, decimal DiscountAmount,
            DateTime StartDate, DateTime EndDate, bool Deleted)
        {
            Discount discount = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountUpdate");
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "UsePercentage", DbType.Boolean, UsePercentage);
            db.AddInParameter(dbCommand, "DiscountPercentage", DbType.Decimal, DiscountPercentage);
            db.AddInParameter(dbCommand, "DiscountAmount", DbType.Decimal, DiscountAmount);
            db.AddInParameter(dbCommand, "StartDate", DbType.DateTime, StartDate);
            db.AddInParameter(dbCommand, "EndDate", DbType.DateTime, EndDate);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                discount = GetByDiscountID(DiscountID);

            return discount;
        }

        public static void AddDiscountToProductVariant(int ProductVariantID, int DiscountID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariant_Discount_MappingInsert");
            db.AddInParameter(dbCommand, "ProductVariantID", DbType.Int32, ProductVariantID);
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static void RemoveDiscountFromProductVariant(int ProductVariantID, int DiscountID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariant_Discount_MappingDelete");
            db.AddInParameter(dbCommand, "ProductVariantID", DbType.Int32, ProductVariantID);
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static DiscountCollection GetDiscountsByProductVariantID(int ProductVariantID, bool showHidden)
        {
            DiscountCollection discountCollection = new DiscountCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountLoadByProductVariantID");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            db.AddInParameter(dbCommand, "ProductVariantID", DbType.Int32, ProductVariantID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Discount discount = new Discount();
                    discount.DiscountID = NopDataHelper.GetInt(dataReader, "DiscountID");
                    discount.Name = NopDataHelper.GetString(dataReader, "Name");
                    discount.UsePercentage = NopDataHelper.GetBoolean(dataReader, "UsePercentage");
                    discount.DiscountPercentage = NopDataHelper.GetDecimal(dataReader, "DiscountPercentage");
                    discount.DiscountAmount = NopDataHelper.GetDecimal(dataReader, "DiscountAmount");
                    discount.StartDate = NopDataHelper.GetDateTime(dataReader, "StartDate");
                    discount.EndDate = NopDataHelper.GetDateTime(dataReader, "EndDate");
                    discount.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    discountCollection.Add(discount);
                }
            }

            return discountCollection;
        }

        public static void AddDiscountToCustomerRole(int CustomerRoleID, int DiscountID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRole_Discount_MappingInsert");
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static void RemoveDiscountFromCustomerRole(int CustomerRoleID, int DiscountID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerRole_Discount_MappingDelete");
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static DiscountCollection GetDiscountsByCustomerRoleID(int CustomerRoleID, bool showHidden)
        {
            DiscountCollection discountCollection = new DiscountCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountLoadByCustomerRoleID");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            db.AddInParameter(dbCommand, "CustomerRoleID", DbType.Int32, CustomerRoleID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Discount discount = new Discount();
                    discount.DiscountID = NopDataHelper.GetInt(dataReader, "DiscountID");
                    discount.Name = NopDataHelper.GetString(dataReader, "Name");
                    discount.UsePercentage = NopDataHelper.GetBoolean(dataReader, "UsePercentage");
                    discount.DiscountPercentage = NopDataHelper.GetDecimal(dataReader, "DiscountPercentage");
                    discount.DiscountAmount = NopDataHelper.GetDecimal(dataReader, "DiscountAmount");
                    discount.StartDate = NopDataHelper.GetDateTime(dataReader, "StartDate");
                    discount.EndDate = NopDataHelper.GetDateTime(dataReader, "EndDate");
                    discount.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    discountCollection.Add(discount);
                }
            }

            return discountCollection;
        }

        public static void AddDiscountToCategory(int CategoryID, int DiscountID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Category_Discount_MappingInsert");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static void RemoveDiscountFromCategory(int CategoryID, int DiscountID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Category_Discount_MappingDelete");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "DiscountID", DbType.Int32, DiscountID);
            db.ExecuteNonQuery(dbCommand);
        }

        public static DiscountCollection GetDiscountsByCategoryID(int CategoryID, bool showHidden)
        {
            DiscountCollection discountCollection = new DiscountCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_DiscountLoadByCategoryID");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Discount discount = new Discount();
                    discount.DiscountID = NopDataHelper.GetInt(dataReader, "DiscountID");
                    discount.Name = NopDataHelper.GetString(dataReader, "Name");
                    discount.UsePercentage = NopDataHelper.GetBoolean(dataReader, "UsePercentage");
                    discount.DiscountPercentage = NopDataHelper.GetDecimal(dataReader, "DiscountPercentage");
                    discount.DiscountAmount = NopDataHelper.GetDecimal(dataReader, "DiscountAmount");
                    discount.StartDate = NopDataHelper.GetDateTime(dataReader, "StartDate");
                    discount.EndDate = NopDataHelper.GetDateTime(dataReader, "EndDate");
                    discount.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    discountCollection.Add(discount);
                }
            }

            return discountCollection;
        }
        #endregion
    }
}
